package com.milorad.ads.data.dao.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import javax.persistence.TypedQuery;

import org.springframework.orm.jpa.JpaCallback;
import org.springframework.transaction.annotation.Transactional;

import com.milorad.ads.data.dao.interfaces.CityDao;
import com.milorad.ads.data.domain.City;

public class CityDaoImpl extends AbstractDaoJPAImpl<City> implements CityDao {

	public CityDaoImpl() {
		super(City.class);		
	}

	@Transactional
	public List<City> findAll() {
		return getJpaTemplate().execute(new JpaCallback<List<City>>() {
			public List<City> doInJpa(EntityManager em) throws PersistenceException {
				TypedQuery<City> query = em.createQuery("select c from City c", City.class);
				return query.getResultList();
			}
		});
	}

	@Transactional
	public int countAll() {
		return getJpaTemplate().execute(new JpaCallback<Integer>() {

			public Integer doInJpa(EntityManager em) throws PersistenceException {
				TypedQuery<Long> query = em.createQuery("select count(c) from City c", Long.class);
				return (query.getSingleResult()).intValue();
			}
		});
	}
}